package com.lc1993929.shiro.action;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * Created by LiuChang on 2017/5/18.
 */
@Controller
public class LogonAction {

    @RequestMapping("/logon")
    public String logon(@RequestParam("username") String username, @RequestParam("password") String password) {
        //1、创建subject实例
        Subject currentUser = SecurityUtils.getSubject();
        //2、判断当前用户是否登录
        if (currentUser.isAuthenticated() == false) {
            //3、将用户名及密码封装UsernamePasswordToken
            UsernamePasswordToken token = new UsernamePasswordToken(username, password);
            try {
                currentUser.login(token);
            } catch (AuthenticationException e) {
                return "error";
            }
        }
        return "success";
    }
}
